<template>
  <section class="page-container">
    <DynamicForm :config="dyConfig" :questions="dyQuestions" />
    <DynamicTable :config="dyTableConfig" :tables="dyTable" :icons="dyIcons">
      <template #borrowCode="{ text, record }">
        <span class="text-blue-400 cursor-pointer" @click="handleShow(record)">{{ text }}</span>
      </template>
    </DynamicTable>
  </section>
</template>

<script lang="ts" setup>
import { reactive, getCurrentInstance, onBeforeUnmount } from 'vue';
import { DynamicForm } from '@/components/public-template';
import { DynamicTable } from '@/components/public-template';
import indexService from './index.service';
import { Api } from '@/config/api/archivesToReturn';
import { filter } from 'rxjs/operators';
import { message } from 'ant-design-vue';
import router from '@/router';

const { proxy }: any = getCurrentInstance();

const dyQuestions: any = reactive(indexService.getQuestions());
const dyConfig: any = {
  labelCol: 6,
  wrapperCol: 18,
  grid: 8,
  style: 'table-form',
  cancelSave: true
};

const dyTable: any = reactive(indexService.getTables());
const dyIcons: any = reactive(indexService.getIcons());
const dyTableConfig: any = {
  queryUrl: Api.archivesBorrowToBeReturnList,
  protType: 'post',
  isScreen: true,
  rowSelection: {
    type: 'checkbox'
  },
  scroll: {
    x: '1500px'
  }
};

const subscribeSub = proxy.$NotificationPubSub
  .getParams()
  .pipe(filter((res) => res !== 0))
  .subscribe((item: any) => {
    if (item.state.selected.length && item.key === 'DYNAMICLOCALICONS') {
      let typeArr: Array<Number> = []; // 借阅类型
      let idArr: Array<String> = []; // id多选
      for (let i of item.state.selected) {
        typeArr.push(i.borrowType);
        idArr.push(i.id);
      }

      if (item.value.value === '档案续借') {
        const arr = item.state.selected.filter((res) => res.status === '归还中');
        console.log(arr);
        if (arr.length > 0) {
          const str = arr.map((item) => {
            return item.filesBoxCode;
          });
          message.warn(`错误档案编码${str}，已发起归还申请！`);
          return;
        }
        // 是否续借是否全为“否"
        window.sessionStorage.setItem('ids', JSON.stringify(idArr));
        router.push({ name: 'ArchivesManageAddRenew', query: { showType: 'ADD', location: 'toReturned' } });
      } else if (item.value.value === '档案归还') {
        const arr = item.state.selected.filter((res) => res.status === '归还中');
        let renewalCode = [],
          flag = false;
        const renewalArr = item.state.selected;
        console.log(arr);
        if (arr.length > 0) {
          const str = arr.map((item) => {
            return item.filesBoxCode;
          });
          message.warn(`错误档案编码${str}，已发起归还申请！`);
          return;
        }
        // 借阅类型是否全为“纸质原件”或“实物”
        if (typeArr.every((item) => item === 1) || typeArr.every((item) => item === 4)) {
          renewalArr.forEach((item) => {
            if (item.isRenew) {
              renewalCode.push(item.filesBoxCode);
              flag = true;
            }
          });
          if (flag) {
            message.warn(`${renewalCode.join(',')}已发起续借申请！`);
            return;
          }
          window.sessionStorage.setItem('ids', JSON.stringify(idArr));
          router.push({ name: 'ArchivesManageAddReturn', query: { showType: 'ADD', location: 'toReturned' } });
        } else {
          message.warn('电子类文件无需发起归还流程');
        }
      }
    } else {
      message.warn('请先选中行');
    }
  });

function handleShow(record) {
  // 根据是否续借跳转，是-档案续借，否-档案借阅
  router.push({
    name: record.isRenew ? 'ArchivesManageAddRenew' : 'ArchivesManageAddBorrow',
    query: {
      id: record.billId,
      showType: 'VIEW'
    }
  });
}

onBeforeUnmount(() => {
  subscribeSub && subscribeSub.unsubscribe();
  proxy.$NotificationPubSub.clearParams();
});
</script>

<style lang="less" scoped></style>
